﻿using Liu_Project.Dal.Common;
using LSL.SqlClient;
using System;
using System.Data;
using System.Text;
using Liu_Project.Utils;

namespace Liu_Project.Dal.Admin
{
    public class EntMenuDal : DalBase
    {
        private readonly StringBuilder sb = new StringBuilder();

        /// <summary>
        /// 获取权限菜单--导航栏
        /// </summary>
        /// <returns></returns>
        public DataTable GetMenuList()
        {
            var sqlHelper = new SQLHelper() { IsAutoClose = false };
            try
            {
                sb.Clear();
                sb.AppendLine("SELECT A.MenuId,A.ParentId,A.MenuCode,A.MenuName,ISNULL(A.Action,'') Action,A.MenuIcon ");
                sb.AppendLine("FROM dbo.EntMenu A WITH(NOLOCK)");
                if (!CurrentUser.IsAdministrator)
                {
                    var tbRoleMenu = Common.RoleHelper.GetRoleMenu(CurrentUser.UserId, sqlHelper);
                    sb.AppendLine("INNER JOIN " + tbRoleMenu + " B ON B.MenuId = A.MenuId");
                }
                sb.AppendLine("WHERE A.Level=1  AND A.IsVisiable=1");
                sb.AppendLine("ORDER BY A.OrderFlag");

                return sqlHelper.ExecuteDataTable(sb.ToString());
            }
            finally
            {
                sqlHelper.Close();
            }
        }

        /// <summary>
        /// 获取二级菜单，操作权限菜单
        /// </summary>
        /// <param name="menuCode"></param>
        /// <returns></returns>
        public DataTable GetMenuLevel_2List(string menuCode)
        {
            var sqlHelper = new SQLHelper() { IsAutoClose = false };
            try
            {
                sb.Clear();
                sb.AppendLine("DECLARE @ParentId NVARCHAR(50)");
                sb.AppendLine("SELECT @ParentId=MenuId FROM dbo.EntMenu WHERE MenuCode=@MenuCode");
                sb.AppendLine("SELECT A.MenuId,A.ParentId,A.MenuCode,A.MenuName,ISNULL(A.Action,'') Action,A.MenuIcon ");
                sb.AppendLine("FROM dbo.EntMenu A WITH(NOLOCK)");
                if (!CurrentUser.IsAdministrator)
                {
                    var tbRoleMenu = Common.RoleHelper.GetRoleMenu(CurrentUser.UserId, sqlHelper);
                    sb.AppendLine("INNER JOIN " + tbRoleMenu + " B ON B.MenuId = A.MenuId");
                }
                sb.AppendLine("WHERE A.Level=2 AND A.IsVisiable=1 AND A.ParentId=@ParentId");
                sb.AppendLine("ORDER BY A.OrderFlag");

                var pList = new SqlParameterList();
                pList.Add("MenuCode", menuCode);

                return sqlHelper.ExecuteDataTable(sb.ToString(), pList);
            }
            finally
            {
                sqlHelper.Close();
            }
        }

        public DataTable GetMenuManagementList(DataTable dt, string sqlWhere, string sortField, string sortOrder, int page, int pageSize, ref int total)
        {
            var sqlHelper = new SQLHelper() { IsAutoClose = false };
            try
            {
                if (dt.Rows.Count == 0)
                    return null;

                string TbSelectMenu = sqlHelper.DataTableToTempTable(dt);
                sqlHelper.ExecuteNonQuery("CREATE INDEX [INDEX_" + TbSelectMenu + "] ON [" + TbSelectMenu + "] (nodeId)");

                string TbMenu = sqlHelper.GetTempTable();
                sb.Clear();
                sb.AppendLine("SELECT A.MenuId,A.ParentId,A.MenuCode,A.MenuName,A.Level,A.OrderFlag");
                sb.AppendLine(",A.Action,ISNULL(A.MenuIcon,'') MenuIcon,A.IsVisiable,A.CreateUserId,A.UpdateUserId");
                sb.AppendLine(",A.CreateTime,A.UpdateTime,C.MenuName ParentMenuName");
                sb.AppendLine(",UC.UserNo CreateUserNo,UC.UserName CreateUserName");
                sb.AppendLine(",UU.UserNo UpdateUserNo,UU.UserName UpdateUserName");
                sb.AppendLine("INTO " + TbMenu);
                sb.AppendLine("FROM dbo.EntMenu A WITH(NOLOCK)");
                sb.AppendLine("JOIN " + TbSelectMenu + " B ON A.MenuId=B.nodeId");
                sb.AppendLine("JOIN dbo.EntMenu C WITH(NOLOCK) ON C.MenuId=A.ParentId");
                sb.AppendLine("LEFT JOIN EntUser UC WITH(NOLOCK) ON A.CreateUserId=UC.UserId");
                sb.AppendLine("LEFT JOIN EntUser UU WITH(NOLOCK) ON A.UpdateUserId=UU.UserId");
                if (!CurrentUser.IsAdministrator)
                {
                    var tbRoleMenu = Common.RoleHelper.GetRoleMenu(CurrentUser.UserId, sqlHelper);
                    sb.AppendLine("INNER JOIN " + tbRoleMenu + " RM ON RM.MenuId = A.MenuId");
                }
                sb.AppendLine("WHERE 1=1 ");
                sb.AppendLine(sqlWhere);
                sqlHelper.ExecuteNonQuery(sb.ToString());

                return PageHelper.GetPageList(TbMenu, "MenuId", "*", sortField, sortOrder, "", pageSize, page, ref total, "");
            }
            finally
            {
                sqlHelper.Close();
            }
        }

        public int UpdateMenuVisiable(string _Data, int _IsVisiable)
        {
            sb.Clear();
            sb.AppendLine("UPDATE dbo.EntMenu SET IsVisiable=@IsVisiable,UpdateTime=GETDATE()");
            sb.AppendLine(",UpdateUserId=@UpdateUserId,UpdateUserName=@UpdateUserName");
            sb.AppendLine("WHERE MenuId IN (" + _Data + ")");
            SqlParameterList pList = new SqlParameterList();
            pList.Add("IsVisiable", _IsVisiable);
            pList.Add("UpdateUserId", CurrentUser.UserId);
            pList.Add("UpdateUserName", CurrentUser.UserName);
            return new SQLHelper().ExecuteNonQuery(sb.ToString(), pList);
        }

        public void UpdateMenu(Entity.Admin.EntMenu entMenu)
        {
            if (entMenu._state == ActionOptions.Edit)
            {
                var item = GetEntityById<Entity.Admin.EntMenu>(entMenu.MenuId.ToString());

                item.MenuName = entMenu.MenuName;
                item.IsVisiable = entMenu.IsVisiable;
                if (!string.IsNullOrWhiteSpace(entMenu.MenuIcon))
                    item.MenuIcon = entMenu.MenuIcon;
                SetUpdateDefaultValue(item);
                UpdateByEntity(item);
            }
        }
    }
}
